package com.sunwayworld.basemodule.coredata.judgmentbasis.service.impl;

import com.sunwayworld.basemodule.coredata.judgmentbasis.bean.LimsJudgmentBasisTestmethodBean;
import com.sunwayworld.basemodule.coredata.judgmentbasis.bean.LimsJudgmentBasisTmAnalyteBean;
import com.sunwayworld.basemodule.coredata.judgmentbasis.bean.LimsJudgmentBasisTmAnalyteHighlowBean;
import com.sunwayworld.basemodule.coredata.judgmentbasis.dao.LimsJudgmentBasisTestmethodDao;
import com.sunwayworld.basemodule.coredata.judgmentbasis.service.LimsJudgmentBasisTestmethodService;
import com.sunwayworld.basemodule.coredata.judgmentbasis.service.LimsJudgmentBasisTmAnalyteHighlowService;
import com.sunwayworld.basemodule.coredata.judgmentbasis.service.LimsJudgmentBasisTmAnalyteService;
import com.sunwayworld.basemodule.coredata.test.bean.LimsTestMethodAnalyteBean;
import com.sunwayworld.basemodule.coredata.test.service.LimsTestMethodAnalyteService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.database.sql.Order;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsJudgmentBasisTestmethodServiceImpl implements LimsJudgmentBasisTestmethodService {

    @Autowired
    private LimsJudgmentBasisTestmethodDao limsJudgmentBasisTestmethodDao;

    @Autowired
    @Lazy
    private LimsTestMethodAnalyteService methodAnalyteService;

    @Autowired
    @Lazy
    private LimsJudgmentBasisTmAnalyteService tmAnalyteService;

    @Autowired
    @Lazy
    private LimsJudgmentBasisTmAnalyteHighlowService highlowService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsJudgmentBasisTestmethodDao getDao() {
        return limsJudgmentBasisTestmethodDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        List<LimsJudgmentBasisTestmethodBean> testmethodBeans = jsonWrapper.parse(LimsJudgmentBasisTestmethodBean.class);
        testmethodBeans.forEach(e -> e.setId(ApplicationContextHelper.getNextIdentity()));
        getDao().insert(testmethodBeans);

        // 关联插入
        List<Long> testMethodIdList = testmethodBeans.stream().map(LimsJudgmentBasisTestmethodBean::getTestMethodId)
                .distinct().collect(Collectors.toList());
        List<LimsTestMethodAnalyteBean> analyteBeanList = methodAnalyteService.selectListByFilter(SearchFilter.instance()
                        .match("testMethodId", testMethodIdList).filter(MatchPattern.OR)
                        .match("analType", "N").filter(MatchPattern.EQ)
                , Order.asc("SORTER"));
        if (!CollectionUtils.isEmpty(analyteBeanList)) {
            List<LimsJudgmentBasisTmAnalyteBean> insertSpAnalyteList = new ArrayList<>();
            for (LimsJudgmentBasisTestmethodBean testMethodBean : testmethodBeans) {
                List<LimsTestMethodAnalyteBean> analyteBeans = analyteBeanList.stream().filter(a ->
                        a.getTestMethodId().equals(testMethodBean.getTestMethodId())).collect(Collectors.toList());
                for (LimsTestMethodAnalyteBean analyteBean : analyteBeans) {
                    LimsJudgmentBasisTmAnalyteBean judgAnalyteBean = new LimsJudgmentBasisTmAnalyteBean();
                    judgAnalyteBean.setId(ApplicationContextHelper.getNextIdentity());
                    judgAnalyteBean.setJudgId(testMethodBean.getJudgId());
                    judgAnalyteBean.setJudgTestId(testMethodBean.getId());
                    judgAnalyteBean.setTestMethodId(testMethodBean.getTestMethodId());
                    judgAnalyteBean.setAnalyte(analyteBean.getAnalyte());
                    judgAnalyteBean.setAnalyteId(analyteBean.getId());
                    judgAnalyteBean.setAnalType(analyteBean.getAnalType());
                    insertSpAnalyteList.add(judgAnalyteBean);
                }
            }
            tmAnalyteService.getDao().insert(insertSpAnalyteList);
        }
        return testmethodBeans.get(0).getId();
    }

    @Override
    @Transactional
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsJudgmentBasisTestmethodBean> parse = wrapper.parse(LimsJudgmentBasisTestmethodBean.class);
        if (CollectionUtils.isEmpty(parse)) return;
        // 级联删除
        List<LimsJudgmentBasisTmAnalyteBean> tmAnalyteBeans = new ArrayList<>();
        List<LimsJudgmentBasisTmAnalyteHighlowBean> tmAnalyteHighlowBeans = new ArrayList<>();
        parse.forEach(e -> {
            LimsJudgmentBasisTmAnalyteBean analyteBean = new LimsJudgmentBasisTmAnalyteBean();
            analyteBean.setJudgTestId(e.getId());
            tmAnalyteBeans.add(analyteBean);
            LimsJudgmentBasisTmAnalyteHighlowBean highlowBean = new LimsJudgmentBasisTmAnalyteHighlowBean();
            highlowBean.setJudgTestId(e.getId());
            tmAnalyteHighlowBeans.add(highlowBean);
        });
        tmAnalyteService.getDao().deleteBy(tmAnalyteBeans, "judgTestId");
        highlowService.getDao().deleteBy(tmAnalyteHighlowBeans, "judgTestId");
        getDao().deleteBy(parse);
    }
}
